﻿using System.Linq;
using Godot;
using KludgeBox.Events;
using KludgeBox.Scheduling;

namespace VectorSurvivors.GameWorld;

[GlobalClass]
public partial class EnemyController : CharacterController
{
    /// <inheritdoc />
    public override Vector2 InputVector => _inputVector;

    /// <inheritdoc />
    public override Vector2 AimPos => _aimPos;
    
    //private static readonly EventPublisher<RequireCurrentWorldRequest> _worldRequester = EventBus.GetPublisher<RequireCurrentWorldRequest>(true);
    
    private Cooldown _aiCooldown;
    private Vector2 _inputVector;
    private Vector2 _aimPos;

    
    public override void _Ready()
    {
        _aiCooldown = new Cooldown(0.2).WithCallback(ProcessAi);
    }

    public override void _Process(double delta)
    {
        _aiCooldown.Update(delta);
    }

    private void ProcessAi()
    {
        var world = WorldHelper.World;
        
        var target = world.Player;

        _aimPos = target.Position;
        _inputVector = _aimPos - Position;
    }
}